/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package visualisar;

import java.awt.Color;
import java.awt.GradientPaint;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;
import resultado.ResultadoHolder;
import traffic.GeradorPanel;

/**
 *
 * @author Leonardo Lino
 */
public class ControladorGraficos {
    
    GeradorPanel gp;
    ResultadoHolder holder;
    
    public ControladorGraficos(GeradorPanel gp){
        this.gp = gp;
        holder = ResultadoHolder.getInstance();
        
        if (gp.jTabbedPane1.getTabCount() > 1){
                gp.jTabbedPane1.remove(2);
                gp.jTabbedPane1.remove(1);
        }
    }
    
    
    public void drawBarChart(){
        final CategoryDataset dataset = createDataset();
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        gp.jTabbedPane1.add("Pedidos/Seg", chartPanel);
    }
    
     private CategoryDataset createDataset() {
         
         final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
         
          // row keys...
        final String series1 = "Sucesso";
        final String series2 = "Falha";
        final String series3 = "Third";
         
         for (int i=1; i <= holder.getSegundos(); i++){
             dataset.addValue(holder.getOKsegundo(i), series1, Integer.toString(i));
             dataset.addValue(holder.getFailSegundo(i), series2, Integer.toString(i));
         }
         
        return dataset;
        
     }
     
     private JFreeChart createChart(final CategoryDataset dataset) {
        
        // create the chart...
        final JFreeChart chart = ChartFactory.createBarChart(
            "Por Segundo",         // chart title
            "Segundos",               // domain axis label
            "Pedidos",                  // range axis label
            dataset,                  // data
            PlotOrientation.VERTICAL, // orientation
            true,                     // include legend
            true,                     // tooltips?
            false                     // URLs?
        );

        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

        // set the background color for the chart...
        chart.setBackgroundPaint(Color.white);

        // get a reference to the plot for further customisation...
        final CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);

        // set the range axis to display integers only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        // disable bar outlines...
        final BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        
        // set up gradient paints for series...
        final GradientPaint gp0 = new GradientPaint(
            0.0f, 0.0f, Color.blue, 
            0.0f, 0.0f, Color.lightGray
        );
        final GradientPaint gp1 = new GradientPaint(
            0.0f, 0.0f, Color.green, 
            0.0f, 0.0f, Color.lightGray
        );
        final GradientPaint gp2 = new GradientPaint(
            0.0f, 0.0f, Color.red, 
            0.0f, 0.0f, Color.lightGray
        );
        renderer.setSeriesPaint(0, gp0);
        renderer.setSeriesPaint(1, gp1);
        renderer.setSeriesPaint(2, gp2);

        final CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(
            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
        );
        // OPTIONAL CUSTOMISATION COMPLETED.
        
        return chart;
        
    }
    
    
    
    public void drawPie(){
        
       // This will create the dataset 
        PieDataset dataset = createDataset2();
        // based on the dataset we create the chart
        JFreeChart chart = createChart(dataset, "Resultado Total");
        // we put the chart into a panel
        ChartPanel chartPanel = new ChartPanel(chart);
        // default size
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        // add it to our application
        gp.jTabbedPane1.add("Total", chartPanel);
        //setContentPane(chartPanel);
    }
    
    
    /**
     * Creates a sample dataset 
     */

    private PieDataset createDataset2() {
        DefaultPieDataset result = new DefaultPieDataset();
        result.setValue("Sucesso", totalOK());
        result.setValue("Fail", totalFail());
        return result;
        
    }
    
    private int totalOK(){
        
        int total = 0;
        for (int i=1; i<= holder.getSegundos(); i++){
            total += holder.getOKsegundo(i);
        }
        
        return total;
    }
    
     private int totalFail(){
        
        int total = 0;
        for (int i=1; i<= holder.getSegundos(); i++){
            
            total += holder.getFailSegundo(i);
        }
        
         
        return total;
    }
    
    /**
     * Creates a chart
     */
    private JFreeChart createChart(PieDataset dataset, String title) {
        
        JFreeChart chart = ChartFactory.createPieChart3D(title,          // chart title
            dataset,                // data
            true,                   // include legend
            true,
            false);

        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        return chart;
        
    }
    
    
}
